//
//  HJInformationTabView.m
//  KaiChat
//
//  Created by lx on 2024/3/11.
//

#import "HJInformationTabView.h"
#import "HJInformationTabCell.h"
#import "HJGradientView.h"
#import "HJNewsTabModel.h"
#import "HJUserNewsTagModel.h"

@interface HJInformationTabView() <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UIButton *moreButton;

@end

@implementation HJInformationTabView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self){
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    
    self.backgroundColor = [UIColor tj_backgroundColor];
    CGFloat btnL = 5;
    CGFloat btnH = 24;
    CGFloat btnR = 20;

    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _flowLayout.minimumLineSpacing = 10;
    _flowLayout.minimumInteritemSpacing = 10;
    _flowLayout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 10);

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
    [_collectionView registerClass:[HJInformationTabCell class] forCellWithReuseIdentifier:@"HJInformationTabCell"];
    _collectionView.collectionViewLayout = _flowLayout;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = UIColor.clearColor;
    _collectionView.frame = CGRectMake(0, 0, self.frame.size.width - btnL - btnH - btnR, self.frame.size.height);
    [self addSubview:_collectionView];

 
    HJGradientView *gradientView = [HJGradientView addToView:self];
    gradientView.colors = @[[UIColorMake(0xFFFFFF) colorWithAlphaComponent:0], UIColorMake(0xFFFFFF)];
    [gradientView setStartPoint:CGPointZero endPoint:CGPointMake(1, 0)];
    gradientView.frame = CGRectMake(self.frame.size.width - 75, 0, 35, self.frame.size.height);
    [gradientView setUserInteractionEnabled:false];
    
    _moreButton = [[UIButton alloc]init];
    [_moreButton setImage:[UIImage imageNamed:@"information_tab_more"] forState:UIControlStateNormal];
    [_moreButton setImage:[UIImage imageNamed:@"information_tab_more"] forState:UIControlStateHighlighted];
    _moreButton.frame = CGRectMake(_collectionView.frame.size.width + btnL, 0, btnH, btnH);
    [_moreButton addTarget:self action:@selector(clickMoreBtn:) forControlEvents:UIControlEventTouchUpInside];
    _moreButton.tj_centerY = _collectionView.tj_centerY;
    [self addSubview:_moreButton];
    
}

- (void)reloadData {
    [_collectionView reloadData];
    return;
}
 
#pragma mark - action
- (void)clickMoreBtn:(UIButton *)sender {
    if(_delegate && [_delegate respondsToSelector:@selector(didClickMore:)]){
        [_delegate didClickMore:self];
    }
}

#pragma mark -UICollectionViewDelegate, UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJInformationTabCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HJInformationTabCell" forIndexPath:indexPath];
    cell.model = self.datas[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    for (HJNewsTabModel *obj in self.datas) {
        obj.isSelect = NO;
    }
    
    HJNewsTabModel *model = self.datas[indexPath.row];
    model.isSelect = YES;

    if(_delegate && [_delegate respondsToSelector:@selector(view:didSelectItem:)]){
        [_delegate view:self didSelectItem:model];
    }
    _selectIndexPath = indexPath;
    [_collectionView reloadData];
    [_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJNewsTabModel *model = self.datas[indexPath.row];
    return model.textSize;
}



@end
